package com.ouc.visualization.service.impl;

import com.ouc.common.utils.ArrayUtils;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.visualization.dao.NcPictDaoVisualization;
import com.ouc.visualization.dao.impl.NcPictDaoVisualizationImpl;
import com.ouc.visualization.entity.PictFile;
import com.ouc.visualization.service.NcPictServiceVisualization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * nc数据转图片相关服务层
 *
 * @author B504
 */
@Service
public class NcPictServiceVisualizationImpl implements NcPictServiceVisualization {

  @Autowired
  NcPictDaoVisualization ncPictDaoVisualization;

  @Async
  @Override
  public Future<BufferedImage> getPictFromNcData2D(float[][] data, float fixedMin, float fixedMax, String colorType, boolean reverseFlag) throws IOException {
    NcPictDaoVisualizationImpl ncPictDao = new NcPictDaoVisualizationImpl();
    if (reverseFlag) {
      data = ArrayUtils.arrReverse(data);
    }
    return new AsyncResult<>(ncPictDao.getPictFromNcData2D(data, fixedMin, fixedMax, colorType));
  }

  @Override
  public Future<BufferedImage> getPictFromNcData2D(float[][] data, String colorType, boolean reverseFlag) throws IOException {
    NcPictDaoVisualizationImpl ncPictDao = new NcPictDaoVisualizationImpl();
    if (reverseFlag) {
      data = ArrayUtils.arrReverse(data);
    }
    return new AsyncResult<>(ncPictDao.getPictFromNcData2D(data, null, null, colorType));
  }

  @Override
  public PictFile getPictFile(String filePath) {
    PictFile pictFile = new PictFile();
    pictFile.setFileName(FileUtils.getName(filePath));
    pictFile.setBaseData(FileUtils.getFileByteArray(new File(filePath)));
    return pictFile;
  }

  @Override
  public BufferedImage getPictFromData2DByThread(float[][] data, Float fixedMin, Float fixedMax, String colorType, boolean reverseFlag) throws IOException, ExecutionException, InterruptedException {
    if (reverseFlag) {
      data = ArrayUtils.arrReverse(data);
    }
    Integer[][] integerData = ncPictDaoVisualization.getColorIndexData(data, fixedMin, fixedMax);
    return ncPictDaoVisualization.getPictFromNcData2DByLine(integerData, colorType);
  }
}
